package org.raymond.iworks.study.mq.rabbitmq.classic.hello;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class Recv {
    public static void main(String[] args) throws Exception {
        String QUEUE_NAME = Send.QUEUE_NAME;
        // 这里不放在try with resource里使资源自动关闭,
        // 因为要保持connection和channel处于活动状态以监听到消息抵达
        Connection conn = Send.connection();
        Channel channel = conn.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        log.info("[*] Waiting for message");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            log.info(" [x] Received {}", message);
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag->{});
    }
}
